﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Minecraft.Data.NBT {
    public class Tag {

        public TagType Type;
        public String Name;
        public Object Payload;
        public List<Tag> Children;
        public TagType ListType;

        public Tag() {
            Children = new List<Tag>();
        }

        public override string ToString() {
            return FormatTag(String.Empty);
        }

        internal string FormatTag(String prefix) {
            StringBuilder dump = new StringBuilder();

            if (Type == TagType.List || Type == TagType.Compound) {
                if(Type == TagType.Compound) 
                    dump.AppendFormat("{0}TAG_{1}(\"{2}\"): {3} entries\n", prefix, Type, Name, Children.Count);
                else
                    dump.AppendFormat("{0}TAG_{1}(\"{2}\"): {3} entries of type TAG_{4}\n", prefix, Type, Name, Children.Count, ListType);

                if (Children != null) {
                    dump.AppendFormat("{0}{{\n", prefix);
                    foreach (var child in Children)
                        dump.Append(child.FormatTag(prefix + "   "));
                    dump.AppendFormat("{0}}}\n", prefix);
                }
            }
            else if (Type == TagType.ByteArray) {
                dump.AppendFormat("{0}TAG_{1}(\"{2}\"): Length {3}\n", prefix, Type, Name, ((Byte[])Payload).Length);
            }
            else {
                dump.AppendFormat("{0}TAG_{1}(\"{2}\"): {3}\n", prefix, Type, Name, Payload);
            }

            return dump.ToString();
        }
    }
}
